<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/05/0005
 * Time: 13:51
 */

namespace app\sports_admin\model;


use think\Config;
use think\Model;
use traits\model\SoftDelete;

class Plan extends Model
{
    use SoftDelete;
    protected $table = 'plan';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    public function getStartPartAttr($value)
    {
        return  json_decode($value,true);
    }
    public function getPreparationPartAttr($value)
    {
        return json_decode($value,true);
    }
    public function getBasicPartAttr($value)
    {
        return json_decode($value,true);
    }
    public function getEndPartAttr($value)
    {
        return json_decode($value,true);
    }
    public function type()
    {
        return $this->belongsTo('plan_type','plan_type_id','id')->field('title,id');
    }
    /**
     * 获取教案信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
    */
    public static function getPlanInfo($page,$rows,$sort,$sortOrder,$searchName)
    {
        $result = self::with('type')->where(function($query)use($searchName){
                if($searchName){
                    $query->where('motion','like',"%{$searchName}%");
                }
            })->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();
        $total = self::with('type')->where(function($query)use($searchName){
            if($searchName){
                $query->where('motion','like',"%{$searchName}%");
            }
        })->count();
        return json(['rows'=>$result,'total'=>$total]);
    }
}